package com.example.springbootsfzy.service.sunnan;

import com.example.springbootsfzy.domain.SelectedArticles;
import com.example.springbootsfzy.mapper.SelectedArticlesMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SelectedArticleService {
    @Autowired
    private SelectedArticlesMapper selectedArticlesMapper;

    /**
     * 分页查询所有文章
     * @param pageNum 当前页码
     * @param pageSize 每页显示的数量
     * @return PageInfo
     */
    public PageInfo<SelectedArticles> getArticles(int pageNum, int pageSize) {
        // 使用 PageHelper.startPage 设置分页
        PageHelper.startPage(pageNum, pageSize);
        List<SelectedArticles> articles = selectedArticlesMapper.getAllArticles();
        return new PageInfo<>(articles);
    }

    /**
     * 添加文章
     * @param selectedArticles 文章对象
     */
    public int addArticle(SelectedArticles selectedArticles) {
        return selectedArticlesMapper.insert(selectedArticles);
    }

    /**
     * 根据id删除文章
     * @param articleId 文章id
     *
     */
    public int deleteById(int articleId) {
        return selectedArticlesMapper.deleteByPrimaryKey(articleId);
    }

    /**
     * 根据id更新文章
     * @param selectedArticles 文章对象
     */
    public int updateBySelectedArticles(SelectedArticles selectedArticles) {
        return selectedArticlesMapper.updateByPrimaryKeySelective(selectedArticles);
    }


    public SelectedArticles getArticle(int articleId) {
        return selectedArticlesMapper.selectByPrimaryKey(articleId);
    }
}
